﻿var Els;
var ElsHid;
var DP_now = new Date();
var DP_year = DP_now.getFullYear();
var DP_month = DP_now.getMonth();
var calendar;

function DP_start() {

    var tmp = "\
<table id=\"frm\" width=\"201\" cellpadding=\"0\" cellspacing=\"1\">\
  <tr>\
    <td>\
	    <table summary=\"calendar\" cellpadding=\"0\" cellspacing=\"0\">\
	    <form>\
	      <tr>\
		      <th>\
            <select name=\"monthS\" onChange=\"DP_chg_date()\">\
							<option value=\"0\"> Jan</option><option value=\"1\"> Feb</option><option value=\"2\"> Mar</option><option value=\"3\"> Apr</option><option value=\"4\"> May</option><option value=\"5\"> Jun</option>\
							<option value=\"6\"> Jul</option><option value=\"7\"> Aug</option><option value=\"8\"> Sep</option><option value=\"9\"> Oct</option><option value=\"10\"> Nov</option><option value=\"11\"> Dec</option>\
            </select>\
            <select name=\"yearS\" onChange=\"DP_chg_date()\"></select>\
		      </th>\
	      </tr>\
	    </form>\
	    </table>\
	  </td>\
	</tr>\
	<tr>\
	  <td>\
	    <table width=\"201\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\
	      <tr>\
		      <td>\
		      <table id=\"oTable\" width=\"100%\" bgcolor=\"#ffffff\" class=\"inCal\">\
						<thead>\
			      <tr class=\"whatDay\">\
			        <th>일</th>\
			        <th>월</th>\
			        <th>화</th>\
			        <th>수</th>\
			        <th>목</th>\
			        <th>금</th>\
			        <th>토</th>\
		        </tr>\
						</thead>\
			      <tbody align=\"center\"></tbody>\
		      </table>\
		      </td>\
	      </tr>\
	    </table>\
	  </td>\
	</tr>\
</table>";
/*
$(function(){
	$("<div id=\"calendar\">" + tmp + "</div>").appendTo('.searchArea');

});
*/
   document.write("<div id=\"calendar\">" + tmp + "</div>");
    DP_print_cal(DP_year, DP_month);
}

function DP_get_lastDate(year, month) {
    var leap;
    var last = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (year % 4 == 0) leap = true;
    if (year % 100 == 0) leap = false;
    if (year % 400 == 0) leap = true;
    if (leap) last[1] = 29;
    return last[month];
}

function DP_get_firstDay(year, month) {
    var ici = new Date(year, month);
    return ici.getDay();
}

function DP_print_cal(year, month) {
    DP_del();
    DP_year = year;
    DP_month = month;

    document.forms[0].yearS.length = 7;
    for (i = 0; i < 7; i++) {
        document.forms[0].yearS.options[i].value = year - 3 + i;
        document.forms[0].yearS.options[i].text = year - 3 + i + "";
    }
    document.forms[0].yearS.selectedIndex = 3;
    document.forms[0].monthS.selectedIndex = month;

    var firstDay = DP_get_firstDay(year, month);
    var lastDate = DP_get_lastDate(year, month);
    var cnt = 0;

    if (firstDay) {
        oTr = document.getElementById("oTable").insertRow(-1);
        for (i = 0; i < firstDay; i++) oTr.insertCell(-1);
        cnt = i;
    }

    for (i = 0; i < lastDate; i++) {
        if (cnt % 7 == 0) oTr = document.getElementById("oTable").insertRow(-1);
        oTd = oTr.insertCell(-1);
        if (cnt % 7 == 0)
            oTd.innerHTML = (DP_now.getFullYear() == year && DP_now.getMonth() == month && DP_now.getDate() == i + 1) ? "<a href=\"javascript:DP_set(" + year + "," + (month + 1) + "," + (i + 1) + ")\" class='sun'><p>" + (i + 1) + "</p></a>" : "<a href=\"javascript:DP_set(" + year + "," + (month + 1) + "," + (i + 1) + ")\" class='sun'>" + (i + 1) + "</a>";
        else
            oTd.innerHTML = (DP_now.getFullYear() == year && DP_now.getMonth() == month && DP_now.getDate() == i + 1) ? "<a href=\"javascript:DP_set(" + year + "," + (month + 1) + "," + (i + 1) + ")\"><p>" + (i + 1) + "</p></a>" : "<a href=\"javascript:DP_set(" + year + "," + (month + 1) + "," + (i + 1) + ")\">" + (i + 1) + "</a>";
        cnt++;
    }

}

function DP_del() {
    for (i = document.getElementById("oTable").rows.length; i > 1; i--) document.getElementById("oTable").deleteRow(i - 1);
}

function DP_set(year, month, date) {
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    ElsHid.value = year + "." + month + "." + date;
    /*
    if (month == "01") month = "Jan";
    if (month == "02") month = "Feb";
    if (month == "03") month = "Mar";
    if (month == "04") month = "Apr";
    if (month == "05") month = "May";
    if (month == "06") month = "Jun";
    if (month == "07") month = "Jul";
    if (month == "08") month = "Aug";
    if (month == "09") month = "Sep";
    if (month == "10") month = "Oct";
    if (month == "11") month = "Nov";
    if (month == "12") month = "Dec";
    */
    Els.value = year + "." + month + "." + date;

    DP_hidden();
}

function get_objectTop(obj) {
    if (obj.offsetParent == document.body || !obj.offsetParent) return obj.offsetTop;
    else return obj.offsetTop + get_objectTop(obj.offsetParent);
}

function get_objectLeft(obj) {
    if (obj.offsetParent == document.body || !obj.offsetParent) return obj.offsetLeft;
    else return obj.offsetLeft + get_objectLeft(obj.offsetParent);
}

function DP_use(El, ElHid) {
    show_elements("select", calendar);
    Els = El;
    ElsHid = ElHid;

	if (El.getAttribute("id") == "txtStartDate" || El.getAttribute("id") == "txtEndDate") {
//        calendar.style.top = (document.body.clientTop + get_objectTop(El) + 12) + "px"; //12
		$("#calendar").css({'top':'-210px'})
    } else {
//        calendar.style.top = (document.body.clientTop + get_objectTop(El) - 149) + "px"; // -149
		$("#calendar").css({'top':'-210px'})
    }
    calendar.style.left = (document.body.clientLeft + get_objectLeft(El) + -285) + "px"; //35


    if (calendar.style.display == "inline") {
        DP_hidden();
    }
    else {
        calendar.style.display = "inline";
        hide_elements("select", calendar);
    }
}

function DP_hidden() {
    show_elements("select", calendar);
    calendar.style.display = "none";
}

function DP_chg_date() {
    DP_year = parseInt(document.forms[0].yearS.value);
    DP_month = parseInt(document.forms[0].monthS.value);
    DP_print_cal(DP_year, DP_month);
}

function hide_elements(tagName, menu) {
    windowed_element_visibility(tagName, -1, menu)
}

function show_elements(tagName, menu) {
    windowed_element_visibility(tagName, +1, menu)
}

function windowed_element_visibility(tagName, change, menu) {
    var els = document.getElementsByTagName(tagName);
    var rect = new element_rect(menu)
    for (i = 0; i < els.length; i++) {
        var el = els.item(i);
        if (elements_overlap(el, rect)) {
            if (change == -1) {
                if (el.name != 'yearS' && el.name != 'monthS') el.style.visibility = "hidden";
            } else el.style.visibility = "visible";
        }
    }
}

function element_rect(el) {
    var left = 0
    var top = 0
    while (el) {
        left += el.offsetLeft
        top += el.offsetTop
        el = el.offsetParent
    }
    this.left = left;
    this.top = top;
}

function elements_overlap(el, rect) {
    var r = new element_rect(el);
    return ((r.left < rect.left + rect.width) && (r.left + r.width > rect.left) && (r.top < rect.top + rect.height) && (r.top + r.height > rect.top))
}
DP_start();


function DP_use_ck(obj, objhid) {
    calendar = document.getElementById("calendar");

    if (calendar.style.display == "none" || calendar.style.display == "") {
        DP_year = DP_now.getFullYear();
        DP_month = DP_now.getMonth();
        DP_print_cal(DP_year, DP_month);
        DP_use(obj, objhid);
    }
    else {
        DP_hidden();
    }
    return false;
}